package com.btt.ff14Tools.controller;

import com.btt.ff14Tools.common.DataLoader;
import com.btt.ff14Tools.dao.HuanKaDao;
import com.btt.ff14Tools.entity.HuanKa;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

@FXMLController
@Slf4j
public class HuankaController implements Initializable{
    @FXML
    private ImageView imgCard;
    @FXML
    private Label labelMethod;
    @FXML
    private AnchorPane huankaView;
    @FXML
    private CheckBox cbIsObtained;
    @FXML
    private TableView<HuanKa> huankaTable;
    @FXML
    private TableColumn<?, ?> colMethod;
    @FXML
    private TableColumn<?, ?> colDown;
    @FXML
    private TableColumn<?, ?> colRare;
    @FXML
    private TableColumn<?, ?> colTop;
    @FXML
    private TableColumn<?, ?> colLeft;
    @FXML
    private TableColumn<?, ?> colRight;
    @FXML
    private TableColumn<?, ?> colIsObtained;
    @FXML
    private TableColumn<?, ?> colIndex;
    @FXML
    private TableColumn<?, ?> colCardName;
    @FXML
    private TableColumn<?, ?> colOrg;
    @FXML
    private TableColumn<?, ?> colDataPatch;
    @FXML
    private Label labelCount;

    @Autowired
    HuanKaDao huanKaDao;

    //幻卡列表
    private static List<HuanKa> hkList;
    //已获取的幻卡编号集合
    private static Set<String> hkObtainedSet;
    //被选中的幻卡编号
    private static Integer selectedIndex;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.hkList = huanKaDao.getAll();
        this.selectedIndex = 1;
        this.hkObtainedSet = huanKaDao.getObtainedSet();
        this.colIndex.setComparator(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                String h1 = (String) o1;
                String h2 = (String) o2;
                try {
                    Integer temp1 = Integer.valueOf(h1);
                    Integer temp2 = Integer.valueOf(h2);
                    return temp1.compareTo(temp2);
                }catch (Exception e){
                    return h1.compareTo(h2);
                }
            }
        });
        //显示表格内容
        showList();
        //显示首条详情
        showDetail(hkList.get(0));
        //添加监听
        addListeners();
    }

    public void showList(){
        this.colIndex.setCellValueFactory(new PropertyValueFactory<>("sIndex"));
        this.colMethod.setCellValueFactory(new PropertyValueFactory<>("methods"));
        this.colDataPatch.setCellValueFactory(new PropertyValueFactory<>("dataPatch"));
        this.colOrg.setCellValueFactory(new PropertyValueFactory<>("org"));
        this.colCardName.setCellValueFactory(new PropertyValueFactory<>("cardName"));
        this.colRare.setCellValueFactory(new PropertyValueFactory<>("rare"));
        this.colTop.setCellValueFactory(new PropertyValueFactory<>("top"));
        this.colLeft.setCellValueFactory(new PropertyValueFactory<>("left"));
        this.colDown.setCellValueFactory(new PropertyValueFactory<>("down"));
        this.colRight.setCellValueFactory(new PropertyValueFactory<>("right"));
        this.colIsObtained.setCellValueFactory(new PropertyValueFactory<>("isObtainedStr"));

        //ObservableList<HuanKa> hkObList = FXCollections.observableList(new ArrayList<>(hkList));
        ObservableList<HuanKa> hkObList = FXCollections.observableList(hkList);
        this.huankaTable.setItems(hkObList);

        updataProgress();
    }

    private void updataProgress(){
        Platform.runLater(()->{
            this.labelCount.setText(String.valueOf(hkObtainedSet.size())+"/"+String.valueOf(hkList.size()));
            this.huankaTable.refresh();
        });
    }

    //添加监听
    private void addListeners(){
        //表格单行点击监听
        this.huankaTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<HuanKa>() {
            @Override
            public void changed(ObservableValue<? extends HuanKa> observable, HuanKa oldValue, HuanKa newValue) {
                selectedIndex = newValue.getIndex();
                showDetail(newValue);
                log.info(newValue.getCardName() + newValue.getIsObtained());
            }
        });
        //勾选框时监听
        this.cbIsObtained.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                log.info(String.valueOf(selectedIndex));
                if(cbIsObtained.isSelected()){
                    hkObtainedSet.add(String.valueOf(selectedIndex));
                } else{
                    hkObtainedSet.remove(String.valueOf(selectedIndex));
                }
                DataLoader.rewriteSet(hkObtainedSet);
                for(HuanKa hkOb:hkList) {
                    if (selectedIndex == hkOb.getIndex()) {
                        hkOb.setIsObtained(cbIsObtained.isSelected());
                        break;
                    }
                }
                updataProgress();
            }
        });
    }

    private void showDetail(HuanKa hk){
        Platform.runLater(()->{
            //显示获取说明
            String methodText = hk.getMethods();
            if(StringUtils.isEmpty(methodText)){
                methodText = "初始获得";
            }
            //图片显示加载
            imgCard.setImage(new Image(hk.getImgUrlB()));
            labelMethod.setText(methodText);
            cbIsObtained.setSelected(hk.getIsObtained());
        });
    }
}